package com.hopu.controller;


import com.alibaba.fastjson.JSON;
import com.feng.common.controller.CommonController;
import com.feng.common.util.Result;
import com.feng.common.util.ResultPage;
import com.hopu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;


@Controller
@RequestMapping("/user")
public class UserConrtoller extends CommonController {

    @Autowired
    UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public String login(@RequestParam Map map, HttpSession session)throws Exception{

        System.out.println("进入了登录的方法");
        System.out.println("map="+map);
        boolean bool =map.isEmpty();
        System.out.println("bool="+bool);
        if (!bool) {
            System.out.println("进来了登录的判断");
            if (userService.login(map)) {
                session.setAttribute("username", map.get("user_account"));

                return this.return_success("1", "success", null);
            }
        }
        return this.return_error();
    }


    @RequestMapping("/query")
    @ResponseBody
    public String shopInfo_query(@RequestParam Map map) throws Exception {
        Result result = new Result();
        //分页
        String page = map.get("page").toString();
        String limit = map.get("limit").toString();
        page = (Integer.valueOf(page)-1)*Integer.valueOf(limit)+"";
        map.put("page",page);
        result.setCode("0");
        result.setData(userService.userquery(map));
        result.setCount(userService.usercount(map).get("count")+"");
        return JSON.toJSONString(result);
    }


    /**
     * 修改
     * @param map
     * @return
     */
    @RequestMapping("/upd")
    @ResponseBody
    public String upd(@RequestParam Map map) throws Exception {
        System.out.println("mao1="+map);
        Result result = new Result();
        result.setCode("1");
        result.setData(Collections.singletonList(userService.userupd(map)));
        return JSON.toJSONString(result);


    }

    /**
     * 新增
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping("/sava")
    @ResponseBody
    public String sava(@RequestParam Map map)throws Exception{
        System.out.println("进入新增的方法");
        System.out.println("map="+map);
        Result result = new Result();
        result.setCode("1");
        result.setData(Collections.singletonList(userService.usersave(map)));
        result.setShop_info_id(map.get("shop_info_id"));

        return JSON .toJSONString(result);
    }

    /**
     * 删除
     */

    @RequestMapping("/del")
    @ResponseBody
    public String del(@RequestParam Map map)throws Exception{
        System.out.println("进入了删除的方法");
        Result result = new Result();
        result.setCode("1");
        result.setData(Collections.singletonList(userService.userdel(map)));
        return  JSON .toJSONString(result);
    }





    @RequestMapping("let")
    @ResponseBody
        public String let(){
        System.out.println("进入了左侧列表");
        List<Map<String ,Object>> list= new ArrayList();
        Map<String,Object> map=new HashMap();
        map.put("name","类目表");
        map.put("url","type/index.html");
        list.add(map);
        Map<String,Object> map1=new HashMap();
        map1.put("name","信息表");
        map1.put("url","inof/index.html");
        list.add(map1);
        Map<String,Object> map2=new HashMap();
        map2.put("name","用户表");
        map2.put("url","user/index.html");
        list.add(map2);
        Result result=new Result();
        result.setData(list);
        result.setCode("0");
        return JSON.toJSONString(result);
        }
}
